<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;


class Index extends Controller
{
    // 首页
    public function index()
    {
        // 获取session
    	$session = Session::get();

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 获取图片
        $picture = DB::table(['picture' => 'p','hmy_user' => 'u'])
                    ->field('p.id,p.cate_id,p.title,p.src,u.icon,u.name,p.user_id')
                    ->where('u.id = p.user_id && p.status = 2')
                    ->order('upload_time')
                    ->select();


        // 返回
        return view('index@index/index',[
            'session'   => $session,
            'category'  => $category,
            'picture'   => $picture,
        ]);
    }


    // 列表
    public function lists($id)
    {
        // 获取session
        $session = Session::get();
        $user = Session::get('hmy_user');

        // var_dump($uid);

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);

        // 查询分类
        $ress = DB::table('category')->where('id',$id)->select();

        // 二级分类
        $a = '0,'.$id.',';
        $res = DB::table('category')->where('path',$a)->select();


        // 图片列表遍历
        $picture = DB::table(['picture' => 'p','hmy_user' => 'u'])
                    ->field('p.id,p.cate_id,p.title,p.src,u.icon,u.name,p.user_id')
                    ->where('u.id = p.user_id && p.cate_id = '.$id.'&& p.status = 2')
                    ->order('upload_time')
                    ->select();

        // 判断是否登陆
        if ($user != null) {

            foreach ($picture as $k => $v) {

                $uid = $session['hmy_user']['id'];


                // 用每张图片的用户id,查出自己有没有关注
                $follow = Db::table(['hmy_user' => 'u','follow' => 'f','users_follow' => 'uf'])
                        ->where('f.id = uf.fid && u.id = f.user_id && f.user_id = '.$v['user_id'].' && uf.uid = '.$uid)
                        ->select();


                if ($follow != null) {

                    $picture[$k]['follow'] = 1;
                }

                // 查询图片是否有收藏
                $like = Db::table(['like' => 'l','picture' => 'p'])
                        ->where('l.user_id = '.$uid.' && l.pict_id = p.id && l.pict_id = '.$v['id'])
                        ->select();

                // 插入数据
                if ($like != null) {

                    $picture[$k]['like'] = 1;
                }


            }

        }

        // 返回
        return view('index@index/list',[
            'session' => $session,
            'category' => $category,
            'res' => $res,
            'ress' => $ress,
            'picture' => $picture
        ]);


    }



    // 三级分类
    public function sanji($path=null,$id=null)
    {
        $session = Session::get();
        $user = Session::get('hmy_user');
        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);


        // 二级分类
        $res = DB::table('category')->where('path',$path)->select();
        $a = count($res);
        // 三级分类
        $c = DB::table('category')->where('path','like',$path.'%,')->select();
        // 图片列表遍历
        $picture = DB::table(['picture' => 'p','hmy_user' => 'u'])
                    ->field('p.id,p.cate_id,p.title,p.src,u.icon,u.name,p.user_id')
                    ->where('u.id = p.user_id && p.cate_id = '.$id.'&& p.status = 2')
                    ->order('upload_time')
                    ->select();

        // var_dump($picture);



        // 判断是否登陆
        if ($user != null) {

            foreach ($picture as $k => $v) {

                $uid = $session['hmy_user']['id'];


                // 用每张图片的用户id,查出自己有没有关注
                $follow = Db::table(['hmy_user' => 'u','follow' => 'f','users_follow' => 'uf'])
                        ->where('f.id = uf.fid && u.id = f.user_id && f.user_id = '.$v['user_id'].' && uf.uid = '.$uid)
                        ->select();


                if ($follow != null) {

                    $picture[$k]['follow'] = 1;
                }

                // 查询图片是否有收藏
                $like = Db::table(['like' => 'l','picture' => 'p'])
                        ->where('l.user_id = '.$uid.' && l.pict_id = p.id && l.pict_id = '.$v['id'])
                        ->select();

                // var_dump($like);


                // 插入数据
                if ($like != null) {

                    $picture[$k]['like'] = 1;
                }


            }

        }



        return view('index@index/sanji',[
            'session' => $session,
            'res' => $res,
            'c' => $c,
            'category' => $category,
            'picture' => $picture,
            'a' => $a
        ]);
    }


    // 详情
    public function detail($id)
    {
        $session = Session::get();

        // 一级分类
        $category = DB::name('category')->where('pid','0,')->paginate(4);

        // 查询图片详情
        $picture = DB::table(['picture' => 'p','hmy_user' => 'u','category' => 'c'])
                    ->field('p.id,p.cate_id,p.title,p.src,u.name,p.abstruct,u.icon,p.user_id')
                    ->where('u.id = p.user_id && p.cate_id = c.id && p.id = '.$id)
                    ->order('upload_time')
                    ->find();
        // var_dump($id);die;


        $like = null;
        $follow = null;

        $se = Session::get('hmy_user');

        // 判断是否登录
        if ($se != null) {

            // 查询是否收藏
            $like = Db::table(['like' => 'l','hmy_user' => 'u','picture' => 'p'])
                    ->where('l.user_id = u.id && l.pict_id = '.$id.' && p.id = '.$id.' && l.user_id = '.$se['id'])
                    ->field('p.id,p.abstruct,p.src,p.title')
                    ->find();


            $uid = $session['hmy_user']['id'];


            // 查询是否关注作者
            $follow = Db::table(['users_follow' => 'uf','follow' => 'f'])
                    ->where('uf.fid = f.id && uf.uid = '.$uid.' && f.user_id = '.$picture['user_id'])
                    ->find();

        }

        // 查询评论
        $comment = DB::table(['picture' => 'p','comment' => 'c','hmy_user' => 'u'])
                    ->field('p.id,u.name,u.icon,c.content,c.commenttime,c.reply')
                    ->where('p.id = c.pid && u.id = c.uid && p.id = '.$id)
                    ->order('commenttime')
                    ->select();


        // 返回跳转
        return view('index@index/detail',[
            'session'   => $session,
            'category'  => $category,
            'picture'   => $picture,
            'comment'   => $comment,
            'like'      => $like,
            'follow'    => $follow
        ]);
    }




     // 评论
    public function comment(Request $request)
    {

        $session = Session::get();
        if ($session == null) {

            $info['status'] = false;
            $info['msg'] = '您还没有登录账号,请先登录!';

            return json($info);

        }

        // 获取用户id
        $id = $session['hmy_user']['id'];


        // 接收数据
        $get = $request->get();



        // 定义数据
        $data['uid'] = $id;
        $data['content'] = $get['content'];
        $data['pid'] = $get['id'];
        $data['name'] = $get['name'];
        $data['commenttime'] = date('Y-m-d H:i:s');


        // 插入sql
        $result = Db::table('comment')->data($data)->insert();

       // 跳转
       if ($result > 0) {

            $info['status'] = true;
            $info['msg'] = '评论成功!';

            return json($info);

       }else{

            $info['status'] = false;
            $info['msg'] = '评论失败!';

            return json($info);
       }

    }


// 首页的模糊搜索
    public function online(Request $request)
    {
        $session = Session::get();
        $category = DB::name('category')->where('pid','0,')->paginate(4);

        // 接搜数据
        $data = $request->get();
        if ($data['name'] != null) {

            $a = $data['name'];
            $a = '%'.$a.'%';
            $res = Db::table(['category'=>'c'])
                        ->whereor('c.name','like',$a)
                        ->select();
        }else{
            $res = null;
        }

        return view('index@index/online',[
            'session' => $session,
            'category' => $category,
            'res' => $res
        ]);


    }



}

